package cn.zhoujing.demo.redis.common.dto;

import cn.zhoujing.demo.redis.enums.AuthorizedGrantTypeEnum;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import javax.validation.constraints.NotBlank;
import java.io.Serializable;

/**
 * @author zhoujing
 * @createTime 2023/12/1 - 18:09
 */
public class LoginDTO implements Serializable {

    /**
     * 客户端id
     */
    @NotBlank(message = "客户端id不能为空")
    private String clientId;

    /**
     * 客户端密码
     */
    @NotBlank(message = "客户端密码不能为空")
    private String clientSecret;

    /**
     * 授予类型
     * <p>
     * password = 密码
     * authorization_code = 授权码
     * client_credentials = 凭证
     * refresh_token = 刷新Token
     * {@link  AuthorizedGrantTypeEnum} 参考
     */
    @NotBlank(message = "授予类型不能为空")
    private String grantType;

    /**
     * 用户id
     * <p>
     * password 时不能为空
     */
    private String userName;

    /**
     * 用户密码
     * <p>
     * 授予类型 = password 时不能为空
     */
    private String password;

    /**
     * 授权码
     * <p>
     * 授予类型 = authorization_code 时不能为空
     */
    private String code;

    /**
     * 重定向URL
     * <p>
     * 授予类型 = code 时不能为空
     */
    private String redirectUri;

    /**
     * 要对Account_Token刷新的Refresh_Token
     * <p>
     * 授予类型 = refresh_token 时不能为空
     */
    private String refreshToken;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getClientId() {
        return clientId;
    }

    public void setClientId(String clientId) {
        this.clientId = clientId;
    }

    public String getClientSecret() {
        return clientSecret;
    }

    public void setClientSecret(String clientSecret) {
        this.clientSecret = clientSecret;
    }

    public String getGrantType() {
        return grantType;
    }

    public void setGrantType(String grantType) {
        this.grantType = grantType;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getRedirectUri() {
        return redirectUri;
    }

    public void setRedirectUri(String redirectUri) {
        this.redirectUri = redirectUri;
    }

    public String getRefreshToken() {
        return refreshToken;
    }

    public void setRefreshToken(String refreshToken) {
        this.refreshToken = refreshToken;
    }

    @Override
    public String toString() {
        return "LoginDTO{" + "clientId='" + clientId + '\'' + ", clientSecret='" + clientSecret + '\'' + ", grantType='" + grantType + '\'' + ", userName='" + userName + '\'' + ", password='" + password + '\'' + ", code='" + code + '\'' + ", redirectUri='" + redirectUri + '\'' + ", refreshToken='" + refreshToken + '\'' + '}';
    }

    /**
     * 参数合法校验
     */
    public void isEmptyCheck() {
        AuthorizedGrantTypeEnum grantTypeEnum = AuthorizedGrantTypeEnum.getByTypeName(grantType);
        Assert.notNull(grantTypeEnum, "授权类型不合法");
        if (grantTypeEnum == AuthorizedGrantTypeEnum.AUTHORIZATION_CODE) {
            boolean isEmpty = StringUtils.hasText(code) && StringUtils.hasText(redirectUri);
            Assert.isTrue(isEmpty, "授权码不合法");
        } else if (grantTypeEnum == AuthorizedGrantTypeEnum.PASSWORD) {
            boolean isEmpty = StringUtils.hasText(userName) && StringUtils.hasText(password);
            Assert.isTrue(isEmpty, "密码授权不合法");
        } else if (grantTypeEnum == AuthorizedGrantTypeEnum.REFRESH_TOKEN) {
            Assert.isTrue(StringUtils.hasText(refreshToken), "refreshToken授权不合法");
        }
    }

}
